package com.sergio.compress.cp.base;

import java.io.IOException;
import java.util.Arrays;
import java.util.Random;

/**
 * @Author : Sergio.D
 * @Description :
 * @CreateDate ：2023/6/15 10:32
 */
public class RandomSource {

    private Random random;

    {
        random = new Random();
    }

    public static byte[] randomByte(byte[] sourceData, int length) throws IOException {
//        String filePath = "files/dickens";
//        File file = new File(filePath);
//        byte[] sourceData = Files.readAllBytes(file.toPath());

//        int minLength = 5; // 指定要截取的最小长度
        int maxLength = sourceData.length; // 指定要截取的最大长度

        int start = getRandomIndex(sourceData.length - length + 1); // 随机选择起始索引
//        int length = getRandomLength(minLength, maxLength); // 随机选择截取的长度

        byte[] subArray = Arrays.copyOfRange(sourceData, start, start + length);
        return subArray;
    }

    public static int getRandomIndex(int size) {
        return new Random().nextInt(size);
    }

    public int getRandomLength(int minLength, int maxLength) {
        return minLength + random.nextInt(maxLength - minLength + 1);
    }

}
